from __future__ import with_statement

import objc
from Foundation import *
from AppKit import *
from QTKit import *
from MovieKit import EditableMovie
import NSImageExt
import os

# enums and other constants
codecLosslessQuality = 0x0400
codecMaxQuality = 0x3FF
codecMinQuality = 0x000
codecLowQuality = 0x100
codecNormalQuality = 0x200
codecHighQuality = 0x300

QTZeroTime = QTMakeTime(0,0)
kQTFileType3GPP = '3gpp'


class DBMovieMaker(object):
    
    def __init__(self, movie_filename, framerate):
        self.movie_filename = movie_filename
        self.movie = None
        self.framerate = framerate

    def addImages(self, images):
        for image in images:
            self.addImageToMovie(image)
        
    def saveMovie(self):
        # setup the proper export attributes in a dictionary
        mdict = {QTMovieExport: 1, QTMovieExportType: kQTFileType3GPP, QTMovieFlatten: True}
        # write the QTMovie to a 3GPP movie file on disk
        self.movie.writeToFile_withAttributes_(self.movie_filename, mdict)
        self.movie = None
        
    def addImageToMovie(self, image):
        image = image.scale_(0.5)
        duration = QTMakeTime(self.framerate, 600)
        attributes = {QTAddImageCodecType: u'tiff', QTAddImageCodecQuality: codecHighQuality} 
        if not self.movie:
            self.movie = EditableMovie.alloc().init()
        self.movie.addImage_forDuration_withAttributes_(image, duration, attributes)
        

        
        
if __name__ == "__main__":
    MovieMaker().test()
